<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="仪表名称" prop="deptName">
                <el-input
                    v-model="queryParams.search"
                    placeholder="请输入卡号"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="卡状态" prop="cstatus">
                <el-select v-model="queryParams.cstatus" placeholder="卡状态" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_card_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="状态" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_normal_disable"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">充值</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table  v-loading="loading" :data="Lists" border fit highlight-current-row>
            <el-table-column type="selection" width="55" bo align="center" label-width="100px" />
            <el-table-column label="编号" align="center" prop="card_id"  width="50"/>
            <el-table-column label="卡号" align="center" prop="card_code"  />
            <el-table-column label="持卡人" align="center" prop="apparatus_id" >
                <template slot-scope="{row}">
                    {{ row.user_info? row.user_info.nickname : "" }}
                </template>
            </el-table-column>
            <el-table-column label="卡内余额" align="center" prop="remaining_value" />
            <el-table-column prop="cstatus" align="center" label="类型" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_card_status" :value="row.cstatus"/>
                </template>
            </el-table-column>
            <el-table-column prop="water_id" align="center" label="所属设备" width="100">
                <template slot-scope="{row}">
                    {{ row.water_device_info? row.water_device_info.water_title : "" }}
                </template>
            </el-table-column>
            <el-table-column prop="cstatus" align="center" label="卡状态" width="100">
                <template slot-scope="{row}">

                    {{ row.water_card? row.water_card.cstatus : 0 }}
<!--                    <dict-tag :options="dict.type.sys_card_status" :value="row.water_card.cstatus??0"/>-->
                </template>
            </el-table-column>
            <el-table-column prop="type" align="center" label="开始/结束日期" width="100">
                <template slot-scope="{row}">
                    {{ row.start_date | parseTime("{y}-{m}-{d}") }}<br>
                    {{ row.end_date | parseTime("{y}-{m}-{d}") }}
                </template>
            </el-table-column>
            <el-table-column  label="状态"align="center"  prop="status" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_normal_disable" :value="row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="激活时间" align="center" prop="created_time"/>
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="{row}">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(row)"
                    >绑定</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(row)"
                    >解绑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.limit"
            @pagination="getList"
        />

        <!-- 添加或修改部门对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="卡号" prop="card_no">
                    <el-input  v-model="form.card_no" placeholder="请输入卡号"  style="width: 50%"/>
                    <el-button type="primary"  @click="handleEditardNo()">获取卡信息</el-button>
                </el-form-item>

                <el-form-item label="卡状态" prop="cstatus">
                    <el-radio-group v-model="form.cstatus">
                        <el-radio
                            v-for="dict in dict.type.sys_card_status"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_normal_disable"
                            :key="dict.value"
                            :label="dict.value"
                        >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>  <!-- 添加或修改部门对话框 -->

    </div>
</template>
<script>

import { getMemberCardList,getMemberCardDetail,createMemberCard,updateMemberCard} from "@/api/grzq/user_card.js";

import {getFormatDate, parseTime} from "@/utils";
export default {
    name: "user_card",
    dicts:['sys_normal_disable','sys_card_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            loadingRead: true,
            // 显示搜索条件
            showSearch: true,
            // 表格数据
            Lists: [],
            total: 0,
            totalRead: 0,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                search: undefined,
                cstatus: undefined,
                status: undefined,
                page: 1,
                limit: 10,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                community_id: [
                    { required: true, message: "请选择片区", trigger: "blur" }
                ],
                apparatus_id: [
                    { required: true, message: "请选择水机", trigger: "blur" }
                ],
                master_name: [
                    { required: true, message: "仪表名称不能为空", trigger: "blur" }
                ],
                master_sn: [
                    { required: true, message: "仪表编码不能为空", trigger: "blur" }
                ],
                price: [
                    { required: true, message: "水/电单价不能为空", trigger: "blur" }
                ],
                cstatus: [
                    { required: true, message: "单价不能为空", trigger: "blur" }
                ],
            },

        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询部门列表 */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getMemberCardList(this.queryParams);
            that.Lists = data.data;
            that.Lists.forEach(item => {
                item.cstatus = 0
                if (item.water_card){
                    item.cstatus = item.water_card.cstatus
                }
            })
            that.loading = false;
            that.total = data.total;
            that.queryParams.limit = data.per_page || 10;

        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                card_id: undefined,
                apparatus_id: undefined,
                meter_name: undefined,
                meter_sn: undefined,
                price: undefined,
                end_day: undefined,
                remark: undefined,
                amount_total: undefined,
                cstatus: "0",
                status: "1"
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        handleEditardNo(){

            console.log(78)
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加数据";
            this.form.dictType = this.queryParams.dictType;
        },

        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const card_id = row.card_id || this.ids
            getMemberCardDetail({card_id:card_id}).then(response => {
                let data= response.data;
                this.form = data;
                this.form.cstatus = data.cstatus.toString();
                this.form.status = data.status.toString();
                this.open = true;
                this.title = "修改";
            });
        },

        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.card_id != undefined) {
                        updateMemberCard(this.form).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        createMemberCard(this.form).then(response => {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },

    }
};
</script>
